'''
Created on 31/12/2009

@author: emlyn
'''
from google.appengine.ext import db
from google.appengine.api import users
from FullPage import FullPage

class WebsiteRec(db.Model):
    sitename = db.StringProperty(multiline=False)
    owneremail = db.StringProperty(multiline=False)
    prefix = db.StringProperty(multiline=False)
    sitebase = db.StringProperty(multiline=False)
    adminuseremails = db.StringListProperty()
    useremails = db.StringListProperty()
  
    def ForceAndGetWebsiteBySiteName(cls, owneremail, SiteName, SiteBase):
        lwebSite = WebsiteRec.GetWebsiteBySiteName(SiteName)
        if not lwebSite:
            lwebSite = WebsiteRec()
            lwebSite.sitename = SiteName
            lwebSite.sitebase = "http://" + SiteBase
            lwebSite.prefix = SiteBase
            lwebSite.owneremail = owneremail
            lwebSite.put()
        return lwebSite
    ForceAndGetWebsiteBySiteName = classmethod(ForceAndGetWebsiteBySiteName)

    def GetCurrentWebsite(cls, request):
        retval = None
        
        host = request.host
        
        Website_query = WebsiteRec.all().filter('prefix =', host)
        Websites = Website_query.fetch(1)      
        
        if Websites:
            retval = Websites[0]
        
        return retval
    GetCurrentWebsite = classmethod(GetCurrentWebsite)      

    def GetWebsiteBySiteName(cls, SiteName):
        retval = None
        
        Website_query = WebsiteRec.all().filter('sitename =', SiteName)
        Websites = Website_query.fetch(1)      
        
        if Websites:
            retval = Websites[0]
        
        return retval
    GetWebsiteBySiteName = classmethod(GetWebsiteBySiteName)      
    
    def WebsitesAreEqual(self, other):
        return self.sitename == other.sitename
    
    def GetPagesForWebsite(self, amount, offset):
        Pages_Query = FullPage.all().filter('sitename = ', self.sitename)
        Pages = Pages_Query.Fetch(amount, offset)
        return Pages
